/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment.home;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.bean.dataoke.DataokeGoodsDetailBean;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuIndexDataBean;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuIndexNavBean;

import java.util.List;

/**
 * 首页适配器
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class HomeContentAdapter extends RecyclerView.Adapter{

    /**
     * 广告幅类型
     */
    public static final int BANNER = 0;

    /**
     * 频道类型
     */
    public static final int NAV = 1;

    /**
     * 活动类型
     */
    public static final int ACT = 2;
    /**
     * 活动类型
     */
    public static final int TILE_SHORT = 3;

    /**
     * 商品列表
     */
    public static final int LIST = 4;

    /**
     * 秒杀类型
     */
    public static final int SECKILL = 3;

    /**
     * 推荐类型
     */
    public static final int RECOMMEND = 4;

    /**
     * 热卖类型
     */
    public static final int HOT = 5;

    protected List<DataokeGoodsDetailBean> goodsList;
    /**
     * 数据
     */
    private HaodankuIndexDataBean resultBean;

    private Fragment mContext;
    LayoutInflater mLayoutInflater;
    /**
     * 当前类型
     */
    private int currenType = BANNER;
    ListViewHolder listViewHolder;

    public HomeContentAdapter(Fragment mContext, HaodankuIndexDataBean resultBean) {
        this.mContext = mContext;
        this.resultBean = resultBean;
        mLayoutInflater = LayoutInflater.from(mContext.getContext());
    }

    /**
     * 根据视图类型创建ViewHolder
     * @param parent
     * @param viewType 当前的视图类型
     * @return RecyclerView.ViewHolder
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == BANNER) {
            return new BannerViewHolder(mContext,mLayoutInflater.inflate(R.layout.banner_viewpager,null));
        }else if(viewType == NAV) {
            return new NavViewHolder(mContext.getContext(),mLayoutInflater.inflate(R.layout.nav_item,null));
        }else if(viewType == ACT) {
            return new ActViewHolder(mContext.getContext(),mLayoutInflater.inflate(R.layout.act_item,null));
        }else if(viewType == TILE_SHORT) {
            return new TileShortViewHolder(mContext.getContext(),mLayoutInflater.inflate(R.layout.tile_short_item,null));
        }else if(viewType == LIST) {
            listViewHolder = new ListViewHolder(mContext,mLayoutInflater.inflate(R.layout.fragment_home_list,null));
            return listViewHolder;
        }
            //频道

//        else if (viewType == SECKILL){
//            return new SeckillViewHolder(mContext,mLayoutInflater.inflate(R.layout.seckkill_item,null));
//            //推荐
//        }else if (viewType == RECOMMEND){
//            return new RecommendViewHolder(mContext,mLayoutInflater.inflate(R.layout.recommend_item,null));
//            //热卖
//        }else if (viewType == HOT){
//            return new HotViewHolder(mContext,mLayoutInflater.inflate(R.layout.hot_item,null));
//        }
        return null;
    }

    public void setGoodsList(List<DataokeGoodsDetailBean> goodsList) {
        this.goodsList = goodsList;
    }


    /**
     * 绑定数据到ViewHolder
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        /**
         * 轮循广告
         */
        if (getItemViewType(position) == BANNER){
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(resultBean.getBanners());

        }
        /**
         * 频道
         */
        else if (getItemViewType(position) == NAV){
            NavViewHolder navViewHolder = (NavViewHolder) holder;
            HaodankuIndexNavBean bean = new HaodankuIndexNavBean();
            bean.setTitle("话费充值");
            resultBean.getNavs().remove(bean);
            HaodankuIndexNavBean firstBean = resultBean.getNavs().get(0);
            firstBean.setImg("https://shengqianapp.oss-cn-shanghai.aliyuncs.com/mtnav.png");
            firstBean.setTag("HOT");
            firstBean.setTitle("外卖红包");
            HaodankuIndexNavBean secondBean = resultBean.getNavs().get(2);
            secondBean.setImg("https://shengqianapp.oss-cn-shanghai.aliyuncs.com/mtnav.png");
            secondBean.setTitle("商超红包");
            HaodankuIndexNavBean thirdBean = resultBean.getNavs().get(3);
            thirdBean.setImg("https://shengqianapp.oss-cn-shanghai.aliyuncs.com/jkj/bz.png");
            thirdBean.setTitle("群壁纸");


            navViewHolder.setData(resultBean.getNavs());

        }
        /**
         * 活动
         */
        else if (getItemViewType(position) == ACT){
            ActViewHolder actViewHolder = (ActViewHolder) holder;
            actViewHolder.setData(resultBean.getTile_long());

        }
        else if (getItemViewType(position) == TILE_SHORT){
            TileShortViewHolder actViewHolder = (TileShortViewHolder) holder;
            actViewHolder.setData(resultBean.getTile_short());

        }
        else if (getItemViewType(position) == LIST){
            ListViewHolder actViewHolder = (ListViewHolder) holder;
            actViewHolder.initData(goodsList);
//            HttpTools.get(String.format(DA_TAO_KE_GOODS_LIST_URL, 1, "10000"), new Callback() {
//                @Override
//                public void onFailure(@NonNull Call call, @NonNull IOException e) {
//                    Log.e("HaodankuListFragment", String.format(DA_TAO_KE_GOODS_LIST_URL, 1) + e.getMessage());
//                }
//
//                @Override
//                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//                    DataokeGoodsListResBean productListBean = JSON.parseObject(response.body().string(), DataokeGoodsListResBean.class);
//                    if(productListBean.getCode() == 0) {
//
//                        mContext.getActivity().runOnUiThread(() -> {
//                            actViewHolder.setData(productListBean.getData().getList());
//                            listViewHolder = actViewHolder;
//                        });
//
//                    }
//                }
//            });

        }
        /**
         * 列表
         */
//        else if (getItemViewType(position) == LIST){
//            ListViewHolder actViewHolder = (ListViewHolder) holder;
//            actViewHolder.setData(listBean);
//
//        }
//        /**
//         * 秒杀
//         */
//        else if (getItemViewType(position) == SECKILL){
//            SeckillViewHolder seckillViewHolder = (SeckillViewHolder) holder;
//            seckillViewHolder.setData(resultBean.getSeckill_info());
//
//        }
//        /**
//         * 推荐
//         */
//        else if (getItemViewType(position) == RECOMMEND){
//            RecommendViewHolder recommendViewHolder = (RecommendViewHolder) holder;
//            recommendViewHolder.setData(resultBean.getRecommend_info());
//
//        }
//        /**
//         * 热卖
//         */
//        else if (getItemViewType(position) == HOT){
//            HotViewHolder hotViewHolder = (HotViewHolder) holder;
//            hotViewHolder.setData(resultBean.getHot_info());
//        }
    }

//
//    /**
//     * 秒杀
//     */
//    class SeckillViewHolder extends RecyclerView.ViewHolder{
//
//        private Context mContext;
//        private TextView tv_time_seckill;
//        private TextView tv_more_seckill;
//        private RecyclerView rv_seckill;
//        private SeckillRecycleViewAdapter adapter;
//
//        /**
//         * 相差多少时间-毫秒
//         */
//        private long dt = 0;
//        //不断循环
//        private Handler handler = new Handler(){
//            @Override
//            public void handleMessage(Message msg) {
//                super.handleMessage(msg);
//                dt = dt - 1000;
//                //设置具体的时间
//                SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
//                String time = dateFormat.format(new Date(dt));
//                tv_time_seckill.setText(time);
//
//                handler.removeMessages(0);
//                //发送消息，不断减时间
//                handler.sendEmptyMessageDelayed(0,1000);
//                if (dt <= 0){
//                    //把消息移除
//                    handler.removeCallbacksAndMessages(null);
//                }
//            }
//        };
//
//        public SeckillViewHolder(Context mContext, View itemView) {
//            super(itemView);
//            this.mContext = mContext;
//            //初始化布局控件
//            tv_time_seckill = (TextView) itemView.findViewById(R.id.tv_time_seckill);
//            tv_more_seckill = (TextView) itemView.findViewById(R.id.tv_more_seckill);
//            rv_seckill = (RecyclerView) itemView.findViewById(R.id.rv_seckill);
//        }
//
//        public void setData(ResultBeanData.ResultBean.SeckillInfoEntity seckill_info) {
//            //得到数据后，就是设置数据（TextView和RecyclerView）的数据
//            adapter = new SeckillRecycleViewAdapter(mContext,seckill_info.getList());
//            rv_seckill.setAdapter(adapter);
//
//            //设置布局管理器
//            rv_seckill.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
//            //设置item的点击事件
//            adapter.setOnSeckillRecyclerView(new SeckillRecycleViewAdapter.OnSeckillRecyclerView() {
//                @Override
//                public void onItemClick(int position) {
//                    Toast.makeText(mContext,"秒杀"+position,Toast.LENGTH_SHORT).show();
//                    startGoodsInfoActivity();
//                }
//            });
//            //秒杀倒计时-毫秒
//            dt = Integer.valueOf(seckill_info.getEnd_time()) - Integer.valueOf(seckill_info.getStart_time());
//            //进入后1秒钟就去发送这个消息
//            handler.sendEmptyMessageDelayed(0,1000);
//
//        }
//    }
//
//    /**
//     * 推荐
//     */
//    class RecommendViewHolder extends RecyclerView.ViewHolder{
//
//        private final Context mContext;
//        private TextView tv_more_recommend;
//        private GridView gv_recommend;
//        private RecommendGridViewAdapter adapter;
//
//        public RecommendViewHolder(final Context mContext, View itemView) {
//            super(itemView);
//            this.mContext = mContext;
//            tv_more_recommend = (TextView) itemView.findViewById(R.id.tv_more_recommend);
//            gv_recommend = (GridView) itemView.findViewById(R.id.gv_recommend);
//
//            //设置点击事件
//            gv_recommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                @Override
//                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                    Toast.makeText(mContext,"position=="+position,Toast.LENGTH_SHORT).show();
//                    startGoodsInfoActivity();
//                }
//            });
//        }
//
//        public void setData(List<ResultBeanData.ResultBean.RecommendInfoBean> recommend_info) {
//            //有数据了之后，就设置适配器
//            adapter = new RecommendGridViewAdapter(mContext,recommend_info);
//            gv_recommend.setAdapter(adapter);
//
//        }
//    }
//
//    /**
//     * 热门
//     */
//    class HotViewHolder extends RecyclerView.ViewHolder{
//
//        private final Context mContext;
//        private TextView tv_more_hot;
//        private GridView gv_hot;
//        private HotGridViewAdapter adapter;
//
//        public HotViewHolder(final Context mContext, View itemView) {
//            super(itemView);
//            this.mContext = mContext;
//            tv_more_hot = (TextView) itemView.findViewById(R.id.tv_more_hot);
//            gv_hot = (GridView) itemView.findViewById(R.id.gv_hot);
//            //设置item的点击事件
//            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                @Override
//                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                    Toast.makeText(mContext,"position"+position,Toast.LENGTH_SHORT).show();
//                    startGoodsInfoActivity();
//                }
//            });
//        }
//
//        public void setData(List<ResultBeanData.ResultBean.HotInfoBean> hot_info) {
//            //有数据后，就设置GridView的适配器
//            adapter = new HotGridViewAdapter(mContext,hot_info);
//            gv_hot.setAdapter(adapter);
//        }
//    }


    /**
     * 启动商品信息列表页面
     */
    private void startGoodsInfoActivity() {
//        Intent intent = new Intent(mContext, GoodsInfoActivity.class);
//        mContext.startActivity(intent);
    }

    /**
     * 根据position得到不同的类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position){
            case BANNER: //广告幅
                currenType = BANNER;
                break;
            case NAV: //频道
                currenType = NAV;
                break;
            case ACT: //活动
                currenType = ACT;
                break;
            case TILE_SHORT: //活动
                currenType = TILE_SHORT;
                break;
            case LIST: //活动
                currenType = LIST;
                break;
//            case SECKILL: //秒杀
//                currenType = SECKILL;
//                break;
//            case RECOMMEND: //推荐
//                currenType = RECOMMEND;
//                break;
//            case HOT: //热卖
//                currenType = HOT;
//                break;
        }
        return currenType;
    }

    /**
     * 总共有多少个item
     * @return
     */
    @Override
    public int getItemCount() {
        return 5;
    }
}
